#!/usr/bin/env bash

set -eu

cd "$(dirname "$0")/.."

function clone_repo {
	owner=$1
	name=$2
	sha=$3

	path=examples/$name
	if [ ! -d "$path" ]; then
		echo "Cloning $owner/$name"
		git clone "https://github.com/$owner/$name" "$path"
	fi

	pushd "$path" >/dev/null
	actual_sha=$(git rev-parse HEAD)
	if [ "$actual_sha" != "$sha" ]; then
		echo "Updating $owner/$name to $sha"
		git fetch
		git reset --hard "$sha"
	fi
	popd >/dev/null
}

clone_repo numpy numpy 058851c5cfc98f50f11237b1c13d77cfd1f40475
clone_repo django django 01974d7f7549b2dca2a729c3c1a1ea7d4585eb3a
clone_repo pallets flask de464c03e134127140e5622e230790806a133ff9
clone_repo python cpython bb456a08a3db851e6feaefc3328f39096919ec8d

known_failures="$(cat script/known_failures.txt)"

# shellcheck disable=2046
tree-sitter parse -q \
	'examples/**/*.py' \
	$(for file in $known_failures; do echo "!${file}"; done)

example_count=$(find examples -name '*.py' | wc -l)
failure_count=$(wc -w <<<"$known_failures")
success_count=$((example_count - failure_count))
success_percent=$(bc -l <<<"100*${success_count}/${example_count}")

printf \
	"Successfully parsed %d of %d example files (%.1f%%)\n" \
	"$success_count" "$example_count" "$success_percent"
